package com.example.rabbitmqspringboot.controller;

import com.example.rabbitmqspringboot.constant.ConfirmQueueConstant;
import com.example.rabbitmqspringboot.constant.DelayQueueOneConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @auther teacher
 * @date 2024/4/3
 * @project rabbitmqSpringBoot
 */

@RestController
@Slf4j
@RequestMapping("/delay")
public class sendMsgController {


    @Autowired
    private RabbitTemplate rabbitTemplate;






    /*
    * 延迟队列发消息
    * */
    @GetMapping("/send/{message}")
    public String sendMsg(@PathVariable String message){
        log.info("当前时间:{},发送消息给两个TTL队列: {}", LocalDateTime.now(),message);

        rabbitTemplate.convertAndSend(DelayQueueOneConstant.NORMAL_EXCHANGE
                ,DelayQueueOneConstant.NORMAL_ROUTING_A
                ,"消息来自ttl为10s的队列:"+message);


        rabbitTemplate.convertAndSend(DelayQueueOneConstant.NORMAL_EXCHANGE
                ,DelayQueueOneConstant.NORMAL_ROUTING_B
                ,"消息来自ttl为30s的队列:"+message);

        return "hello rabbitmq!";

    }

    @GetMapping("/sendTtlMsg/{message}/{ttl}")
    public String sendMsg(@PathVariable String message,@PathVariable String ttl){

       rabbitTemplate.convertAndSend(DelayQueueOneConstant.NORMAL_EXCHANGE,DelayQueueOneConstant.NORMAL_ROUTING_C,
               message,correlationData->{
                    correlationData.getMessageProperties().setExpiration(ttl);
                    return correlationData;
               });

        log.info("当前时间：{},发送一条时长{}毫秒 TTL 信息给队列 C:{}",
                LocalTime.now(), ttl, message);

        return "hi rabbitmq !!!";

    }

    /**
     * 延迟队列
     *
     * @param message   消息
     * @param delayTime 延迟时间
     */
    @GetMapping("/sendDelayMsg/{message}/{delayTime}")
    public String sendDelayMsg(@PathVariable String message,
                             @PathVariable Integer delayTime) {
        rabbitTemplate.convertAndSend(DelayQueueOneConstant.DELAY_EXCHANGE,
                DelayQueueOneConstant.DELAY_ROUTING_KEY, message, correlationData -> {
                    correlationData.getMessageProperties().setDelay(delayTime);
                    return correlationData;
                });
        log.info("当前时间：{},发送一条延迟{}毫秒的信息给队列 :{}",
                LocalTime.now(), delayTime, message);
        return "hi delay-rabbitmq !!!";
    }
    /**
     * 高级发布确认
     */
    @GetMapping("/sendConfirmMsg/{message}")
    public String sendConfirmMsg(@PathVariable String message) {
        rabbitTemplate.convertAndSend(ConfirmQueueConstant.CONFIRM_EXCHANGE,
                ConfirmQueueConstant.CONFIRM_ROUTING_KEY, message, new CorrelationData("1"));
        log.info("发送一条信息 :{}", message);
        return "hi:sendConfirmMsg";
    }

    /**高级发布确认
     * 交换机找不到路由
     * 消息回退处理
     *
     * @param message
     */
    @GetMapping("/sendConfirmMsg2/{message}")
    public void sendConfirmMsg2(@PathVariable String message) {
        rabbitTemplate.convertAndSend(ConfirmQueueConstant.CONFIRM_EXCHANGE,
                ConfirmQueueConstant.CONFIRM_ROUTING_KEY + "2", message, new CorrelationData("2"));
        log.info("发送一条信息 :{}", message + "，routingKey为：" + ConfirmQueueConstant.CONFIRM_ROUTING_KEY + "2");
    }



}
